package com.hyq.market.myapplication.test.n99;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Administrator on 2017/7/20 0020.
 */

public class JsounpTest9 {

//    p06 qingchun 67  未保存
//    05  meitui 46
  /*for (int i=365 ; i <372; i++) {*/  //已遍历
    private static final  String BaseUeURL="http://www.99nboc.com/rpur_6_2.html";
    private static final String BaseUrl="http://www.99nboc.com";
    private static final String ParentUrl=BaseUrl+"/html/part/index16_19.html";
    private static final String ParentUrl_Auto=BaseUrl+"/html/part/index16_";
    private static final String BaseFilePath="G:"+ File.separator+"img"+
            File.separator+"zipaitoupai"+File.separator;
    private static String dirPath;
    static  ExecutorService threadPool = Executors.newFixedThreadPool(50);
    public static void main(String[] args) throws Exception {
        for (int i=300 ; i <365; i++) {
            final int finalI = i;
            threadPool.execute(new Runnable() {
                public void run() {
                    try {
                        String sdf="http://www.99nboc.com/rpur_6_"+ finalI +".html";
                        dolist(sdf,0);
                        Thread.sleep(5*1000L);
                    }  catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

    }

    /**********
     * 加载列表
     * @param url
     * @throws IOException
     * @param num  异常尝试次数  5次尝试跳出循环
     */
    private static  void dolist(String url,int num)  {
        Elements images = null;
        try {
            Document doc = Jsoup.connect(url).get();
            //  Elements images = doc.select("div.typelist ul >li:first-of-type");
            images = doc.select("div.zxlist ul >li:first-of-type > a");
        } catch (IOException e) {
            e.printStackTrace();
            if (num>2)
                return;
            dolist(url,num+1);
        }

        for (final Element image : images) {
            threadPool.execute(
                    new Runnable() {
                                   @Override
                                   public void run() {
                                       String date =image.parent().parent().select("li:last-of-type > font").text();
                                       dirPath=BaseFilePath+date+File.separator+image.select("a").text()+File.separator;
                                       parseUrl(BaseUrl+image.select("a").attr("href"),dirPath,0);
                                       System.out.println("正在解析网页#####: " + image.select("a").attr("href"));
                                   }
                               }
            );
        }
    }




    /**********
     * 具体网页解析
     * @param url
     * @throws IOException
     * @param num  异常尝试次数  5次尝试跳出循环
     */
    private static void parseUrl(String url,String relateFile,int num) {
        // dirIsExists(relateFile);
        Elements images = null;
        try {
            Document doc = Jsoup.connect(url).get();
            images = doc.select("div.n_bd > img");
        } catch (IOException e) {
            e.printStackTrace();
            if (num>3)
                return;
            parseUrl(url,relateFile,num+1);
        }
        for (int i = 0; i < images.size(); i++) {
            Element image=images.get(i);
            String file=relateFile+ i +".jpg";
            savePic(image.attr("data-original"),file,relateFile,0);
        }
    }

    /*******
     * 保存图片
     * @param imgUrl
     * @param filePath
     * @param dirPath
     * @param num  异常尝试次数  5次尝试跳出循环
     */
    public static void savePic(final String imgUrl, final String filePath, final String dirPath, final int num)  {

        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(imgUrl);
                    HttpURLConnection conn = null;
                    conn = (HttpURLConnection) url.
                            openConnection();
                    conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(5 * 1000);
                    conn.setDoInput(true);
                    conn.setUseCaches(false);
                    InputStream inStream = conn.getInputStream();
                    byte[] data = readInputStream(inStream);

                    dirIsExists(dirPath);
                    File imageFile = new File(filePath);
                    FileOutputStream outStream = new FileOutputStream(imageFile);
                    outStream.write(data);
                    outStream.close();
                    System.out.println("正在保存文件:"+filePath);
                } catch (IOException e) {
                    e.printStackTrace();
                       if (num>10)
                           return;
                       savePic(imgUrl,filePath,dirPath,num+1);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

    }

    public static byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024*1024*10];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while( (len=inStream.read(buffer)) != -1 ){
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }


    public static boolean dirIsExists(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists())
            return true;
        if (!destDirName.endsWith(File.separator)) {
            destDirName = destDirName + File.separator;
        }
        //创建目录
        if (dir.mkdirs()) {
            System.out.println("创建目录" + destDirName + "成功！");
            return true;
        }
        return false;
    }

}
